﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Web;

public partial class Admin_UserContent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (Session["userid"] != null)
            {
                string userId = Session["userid"].ToString();
                string sql = "select * from users where userid = '" + userId + "'";
                DataTable table = DBHelper.GetDataSet(sql);
                ViewState["UserTable"] = table;
                if (table.Rows.Count > 0)
                {
                    lblUserLogin.Text = table.Rows[0]["userlogin"].ToString();
                    if (table.Rows[0]["UserSex"].ToString().Equals("男"))
                    {
                        lblName.Text = "Mr."+table.Rows[0]["UserName"].ToString();
                    }
                    else 
                    {
                        lblName.Text = "Ms." + table.Rows[0]["UserName"].ToString();
                    }
                    lblUserIDCard.Text = table.Rows[0]["UserIDCard"].ToString();
                    lblUserAge.Text = table.Rows[0]["UserAge"].ToString();
                    lblUserSex.Text = table.Rows[0]["UserSex"].ToString();
                    lblMarriage.Text = table.Rows[0]["Marriage"].ToString();
                    lblHandsetNumber.Text = table.Rows[0]["HandsetNumber"].ToString();
                    lblTelephoneNumber.Text = table.Rows[0]["TelephoneNumber"].ToString();
                    lblUserEmail.Text = table.Rows[0]["UserEmail"].ToString();
                    lbllanguage.Text = table.Rows[0]["language"].ToString();



                    //修改信息
                    txtUserLogin.Text = table.Rows[0]["userlogin"].ToString();
                    txtUserIDCard.Text = table.Rows[0]["UserIDCard"].ToString();
                    txtAge.Text = table.Rows[0]["UserAge"].ToString();
                    //rdoSex.SelectedValue = table.Rows[0]["UserSex"].ToString();
                    if (table.Rows[0]["UserSex"].ToString().Equals(this.rdoMan.Text))//男 
                    {
                        this.rdoMan.Checked = true;
                    }
                    else
                    {
                        this.rdoWomen.Checked = true;
                    }
                    //RadioButtonList1.SelectedValue = table.Rows[0]["Marriage"].ToString();
                    if (table.Rows[0]["Marriage"].ToString().Equals(this.rdoHun.Text))
                    {
                        this.rdoHun.Checked = true;
                    }
                    else
                    {
                        this.rdoNoHun.Checked = true;
                    }
                    TextBox3.Text = table.Rows[0]["HandsetNumber"].ToString();
                    TextBox4.Text = table.Rows[0]["TelephoneNumber"].ToString();
                    TextBox5.Text = table.Rows[0]["UserEmail"].ToString();
                    //RadioButtonList2.SelectedValue = table.Rows[0]["language"].ToString();
                    if (table.Rows[0]["language"].ToString().Equals(this.rodlanguage.Text))
                    {
                        this.rodlanguage.Checked = true;
                    }
                    else
                    {
                        this.rodlanguageEnglish.Checked = true;
                    }
                    txtpwd.Text = table.Rows[0]["UserPwd"].ToString();

                    //保存密码和ID

                    ViewState["userId"] = table.Rows[0]["UserId"];
                    ViewState["pass"] = table.Rows[0]["UserPwd"];
                }
                
                getAgency();//验证预约信息
                NewMethod();
            }
            else 
            {
                Web.CommFun.Show_Msg("信息丢失，请重新登录", "index.aspx");
            }
        }
    }
    //修改用户资料
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string userSex = string.Empty;
        string marriage = string.Empty;
        string language = string.Empty;

        if (this.rdoMan.Checked) 
        {
            userSex = this.rdoMan.Text;
        }
        else if (rdoWomen.Checked) 
        {
            userSex = this.rdoWomen.Text;
        }
        if (rdoHun.Checked)
        {
            marriage = this.rdoHun.Text;
        }
        else if(rdoNoHun.Checked)
        {
            marriage = rdoNoHun.Text;
        }
        if (rodlanguage.Checked) 
        {
            language = rodlanguage.Text;
        }
        else if (rodlanguageEnglish.Checked) 
        {
            language = rodlanguageEnglish.Text;
        }

        string sql = string.Empty;
        if (txtpwd.Text.Trim() != "") //重新输入密码
        {
            sql =
               "UPDATE users " +
               "SET " +
                   "UserLogin = '" + txtUserLogin.Text.Trim() + "', " +
                   "UserPwd = '" + txtpwd.Text.Trim() + "', " +
                   "UserIDCard = '" + txtUserIDCard.Text.Trim() + "'," +
                   "UserAge = '" + txtAge.Text.Trim() + "'," +
                   "UserSex = '" + userSex + "'," +
                   "Marriage  = '" + marriage + "'," +
                   "HandsetNumber = '" + TextBox3.Text.Trim() + "'," +
                   "TelephoneNumber = '" + TextBox4.Text.Trim() + "'," +
                   "UserEmail = '" + TextBox5.Text.Trim() + "'," +
                   "language  = '" + language + "'" +

               " WHERE UserID = '" + ViewState["userId"].ToString() + "'";
        }
        else 
        {
             sql =
                "UPDATE users " +
                "SET " +
                    "UserLogin = '" + txtUserLogin.Text.Trim() + "', " +
                    "UserIDCard = '" + txtUserIDCard.Text.Trim() + "'," +
                    "UserAge = '" + txtAge.Text.Trim() + "'," +
                    "UserSex = '" + userSex + "'," +
                    "Marriage  = '" + marriage + "'," +
                    "HandsetNumber = '" + TextBox3.Text.Trim() + "'," +
                    "TelephoneNumber = '" + TextBox4.Text.Trim() + "'," +
                    "UserEmail = '" + TextBox5.Text.Trim() + "'," +
                    "language  = '" + language + "'" +

                " WHERE UserID = '" + ViewState["userId"].ToString() + "'";
        }

       

        int resutl = DBHelper.ExecuteCommand(sql);
        if (resutl > 0)
            Web.CommFun.Show_Msg("修改个人信息成功！", "UserContent.aspx");
        else
            Web.CommFun.Show_Msg("修改个人信息失败，退出再次登录！", "UserContent.aspx");
    }
    //退出
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Session["userid"] = null;
        Web.CommFun.Show_Msg("退出成功，请再次登录！", "index.aspx");
    }
    //预约体检
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        if (ViewState["userId"] != null)
        {
            DateTime time = DateTime.Now;
            DateTime ddltime = DateTime.Parse(ddlTime.SelectedValue);
            TimeSpan ts = ddltime - time;  //得到时间差
            if (ts.TotalDays > 1)
            {

                DataTable userTable = (DataTable)ViewState["UserTable"];//取出用户信息

                if (userTable.Rows[0]["userSex"].ToString() == "" || userTable.Rows[0]["userSex"] == null)
                {
                    Web.CommFun.Show_Msg("您的性别为空，请修改后在预约！");

                }
                else if (userTable.Rows[0]["userage"].ToString() == "" || userTable.Rows[0]["userage"] == null)
                {
                    Web.CommFun.Show_Msg("您的年龄为空，请修改后在预约！");

                }
                else if (userTable.Rows[0]["Marriage"].ToString() == "" || userTable.Rows[0]["Marriage"] == null)
                {
                    Web.CommFun.Show_Msg("您的婚否为空，请修改后在预约！");

                }
                else
                {
                    string address = null;
                    string num = null;
                    if (ddljigou.SelectedValue.Equals("瑞慈体检-陆家嘴分部"))
                    {
                        address = "上海市浦东新区浦东南路256号华夏银行大厦3楼";
                        num = "100人/日（外籍人士<=3人）";
                    }
                    else if (ddljigou.SelectedValue.Equals("瑞慈体检-静安店"))
                    {
                        address = "上海市静安区江宁路212号凯迪克大厦2楼";
                        num = "50人/日（外籍人士<=3人）";
                    }

                    string sql1 = "select * from MedicalAppointment where UserID ='" + ViewState["userId"] + "'";
                    DataTable table1 = DBHelper.GetDataSet(sql1);

                    string setMeal = string.Empty;//体检套餐
                    string sex = userTable.Rows[0]["userSex"].ToString();//性别
                    int age = Convert.ToInt32(userTable.Rows[0]["userAge"].ToString());//年龄
                    string marriage = userTable.Rows[0]["Marriage"].ToString();

                    if (age < 40 && sex.Equals("男"))
                    {
                        setMeal = "A套餐";
                    }
                    if (age >= 40 && sex.Equals("男"))
                    {
                        setMeal = "B套餐";
                    }
                    if (age < 40 && sex.Equals("女") && marriage.Equals("未婚"))
                    {
                        setMeal = "C套餐";
                    }
                    if (age >= 40 && sex.Equals("女") && marriage.Equals("未婚"))
                    {
                        setMeal = "D套餐";
                    }
                    if (age < 40 && sex.Equals("女") && marriage.Equals("已婚"))
                    {
                        setMeal = "E套餐";
                    }
                    if (age >= 40 && sex.Equals("女") && marriage.Equals("已婚"))
                    {
                        setMeal = "F套餐";
                    }

                    if (table1.Rows.Count > 0) //有预约体检
                    {
                        string sql = "update MedicalAppointment set ReservationTime = '" + this.ddlTime.SelectedValue + "',SetMeal='" + setMeal + "',AgencyName='"
                            + ddljigou.SelectedValue + "',AgencyAddress='" + address + "',ReservationNum='" + num + "',BookingStatus=0 where userid = '" + ViewState["userId"] + "'";
                        DBHelper.ExecuteCommand(sql);
                    }
                    else
                    {
                        string sql = "insert into MedicalAppointment values('" + ViewState["userId"].ToString() + "','" + this.ddlTime.SelectedValue + "','" + setMeal + "','"
                            + ddljigou.SelectedValue + "','" + address + "','2010年7月17-8月1','" + num + "','0')";
                        Web.DBHelper.GetScalar(sql);
                    }
                    Web.CommFun.Show_Msg("预约成功！", "UserContent.aspx");
                }
            }
            else 
            {
                Web.CommFun.Show_Msg("请提前两个工作日进行预约！", "UserContent.aspx");
            }
        }
    }


    private void getTime()
    {
        string time = this.ddlTime.SelectedValue;  //获得时间
        string agencyName = this.ddljigou.SelectedValue; //机构名称
        string sql = "select COUNT(*)as counts from MedicalAppointment where ReservationTime ='" + time + "' and AgencyName = '" + agencyName + "' and BookingStatus!=1";
        DataTable tableTime = DBHelper.GetDataSet(sql);
       
        if (tableTime.Rows.Count >= 100) //人数超过上线
        {
            ImageButton3.Enabled = false;
            this.lblZ.Text = "预约人数已满";
        }
    }

    public void getAgency() 
    {
        
        getTime();//验证人数是否已满

        ViewState["userid"] = Session["userid"].ToString();
        string sql = "select * from MedicalAppointment where userid='" + ViewState["userid"].ToString() + "' and BookingStatus != 1";
        DataTable tableSql = DBHelper.GetDataSet(sql);

        if (tableSql.Rows.Count > 0) //验证是否预约
        {
            if (tableSql.Rows.Count > 0)
            {
                DateTime timeNows = DateTime.Parse(tableSql.Rows[0]["ReservationTime"].ToString());//员工预约时间
                DateTime timeNow = DateTime.Now;
                //int time1 = int.Parse((timeNows - timeNow).ToString());
                TimeSpan ts = timeNows - timeNow;
                if (ts.TotalDays < 2)
                {
                    this.ddljigou.Enabled = false;
                    this.ddlTime.Enabled = false;
                    ImageButton3.Enabled = false;
                }

                this.lblStatus.Text = "已预约  Booked";

            }
            if (tableSql.Rows[0]["BookingStatus"].ToString().Equals("2"))
            {
                this.ddljigou.Enabled = false;
                this.ddlTime.Enabled = false;
                ImageButton3.Enabled = false;
                this.lblStatus.Text = "已体检  Completed";
               
            }
            this.ddljigou.SelectedValue = tableSql.Rows[0]["AgencyName"].ToString();
            this.ddlTime.SelectedValue = DateTime.Parse(tableSql.Rows[0]["ReservationTime"].ToString()).ToString("yyyy-MM-dd");
            //this.ddlTime.SelectedValue = tableSql.Rows[0]["ReservationTime"].ToString();
        }
    }
    protected void ddlTime_SelectedIndexChanged(object sender, EventArgs e)
    {
        getTime();
    }

    public void GetBindDDL(DropDownList ddl) 
    {
        ddl.Items.Clear();

        ListItem item = new ListItem();
        item.Text = "2010-07-30";
        item.Value = "2010-07-30";
        ddl.Items.Add(item);

        ListItem item0 = new ListItem();
        item0.Text = "2010-07-31";
        item0.Value = "2010-07-31";
        ddl.Items.Add(item0);

        ListItem item1 = new ListItem();
        item1.Text = "2010-08-01";
        item1.Value = "2010-08-01";
        ddl.Items.Add(item1);

        ListItem item2 = new ListItem();
        item2.Text = "2010-08-02";
        item2.Value = "2010-08-02";
        ddl.Items.Add(item2);

        ListItem item3 = new ListItem();
        item3.Text = "2010-08-03";
        item3.Value = "2010-08-03";
        ddl.Items.Add(item3);

        ListItem item4 = new ListItem();
        item4.Text = "2010-08-04";
        item4.Value = "2010-08-04";
        ddl.Items.Add(item4);


        ListItem item5 = new ListItem();
        item5.Text = "2010-08-05";
        item5.Value = "2010-08-05";
        ddl.Items.Add(item5);


        ListItem item6 = new ListItem();
        item6.Text = "2010-08-06";
        item6.Value = "2010-08-06";
        ddl.Items.Add(item6);


        ListItem item7 = new ListItem();
        item7.Text = "2010-08-07";
        item7.Value = "2010-08-07";
        ddl.Items.Add(item7);


        ListItem item8 = new ListItem();
        item8.Text = "2010-08-08";
        item8.Value = "2010-08-08";
        ddl.Items.Add(item8);


        ListItem item9 = new ListItem();
        item9.Text = "2010-08-09";
        item9.Value = "2010-08-09";
        ddl.Items.Add(item9);


        ListItem item10 = new ListItem();
        item10.Text = "2010-08-10";
        item10.Value = "2010-08-10";
        ddl.Items.Add(item10);

        ListItem item11 = new ListItem();
        item11.Text = "2010-08-11";
        item11.Value = "2010-08-11";
        ddl.Items.Add(item11);

        ListItem item12 = new ListItem();
        item12.Text = "2010-08-12";
        item12.Value = "2010-08-12";
        ddl.Items.Add(item12);

        ListItem item13 = new ListItem();
        item13.Text = "2010-08-13";
        item13.Value = "2010-08-13";
        ddl.Items.Add(item13);

        ListItem item14 = new ListItem();
        item14.Text = "2010-08-14";
        item14.Value = "2010-08-14";
        ddl.Items.Add(item14);

        ListItem item15 = new ListItem();
        item15.Text = "2010-08-15";
        item15.Value = "2010-08-15";
        ddl.Items.Add(item15);
       
    }

    public void GetBindDLL(DropDownList ddl) 
    {
        ddl.Items.Clear();
        ddl.Items.Clear();

        ListItem item = new ListItem();
        item.Text = "2010-07-30";
        item.Value = "2010-07-30";
        ddl.Items.Add(item);

        ListItem item1 = new ListItem();
        item1.Text = "2010-08-02";
        item1.Value = "2010-08-02";
        ddl.Items.Add(item1);

        ListItem item2 = new ListItem();
        item2.Text = "2010-08-03";
        item2.Value = "2010-08-03";
        ddl.Items.Add(item2);

        ListItem item3 = new ListItem();
        item3.Text = "2010-08-04";
        item3.Value = "2010-08-04";
        ddl.Items.Add(item3);

        ListItem item4 = new ListItem();
        item4.Text = "2010-08-05";
        item4.Value = "2010-08-05";
        ddl.Items.Add(item4);


        ListItem item5 = new ListItem();
        item5.Text = "2010-08-06";
        item5.Value = "2010-08-06";
        ddl.Items.Add(item5);


        ListItem item6 = new ListItem();
        item6.Text = "2010-08-09";
        item6.Value = "2010-08-09";
        ddl.Items.Add(item6);



        ListItem item9 = new ListItem();
        item9.Text = "2010-08-10";
        item9.Value = "2010-08-10";
        ddl.Items.Add(item9);


        ListItem item10 = new ListItem();
        item10.Text = "2010-08-11";
        item10.Value = "2010-08-11";
        ddl.Items.Add(item10);

        ListItem item11 = new ListItem();
        item11.Text = "2010-08-12";
        item11.Value = "2010-08-12";
        ddl.Items.Add(item11);

        ListItem item12 = new ListItem();
        item12.Text = "2010-08-13";
        item12.Value = "2010-08-13";
        ddl.Items.Add(item12);

       
        
    }
    protected void ddljigou_SelectedIndexChanged(object sender, EventArgs e)
    {
        NewMethod();
    }

    private void NewMethod()
    {
        if (ddljigou.SelectedValue.Equals("瑞慈体检-静安店"))
        {
            GetBindDLL(ddlTime);
        }
        else if (ddljigou.SelectedValue.Equals("瑞慈体检-陆家嘴分部"))
        {
            GetBindDDL(ddlTime);
        }
    }
}
